import os, glob, re, mimetypes, shutil

import app_settings

class DynamicFile(object):
    root = None
    path = None
    filename = None
    _size = None
    _mimetype = None
    is_directory = False

    def __init__(self, root, filename):
        self.root = os.path.abspath(root)
        filename = os.path.abspath(filename)
        self.path = '/'.join(filename.split('/')[:-1])
        self.filename = filename.split('/')[-1]
        self.is_directory = os.path.isdir(filename)

    def get_files(self):
        files = glob.glob(self.full_path + (self.is_directory and '/' or '') + app_settings.DYNAMIC_PUBLIC_FILES_PATTERN)
        return [DynamicFile(self.root, f) for f in files]

    @property
    def full_path(self):
        return self.path + '/' + self.filename

    @property
    def link_path(self):
        return "%s/%s" %(self.path[len(self.root):], self.filename)

    @property
    def view_link(self):
        path = self.link_path

        if self.is_directory:
            return '?f=%s'%path
        else:
            return 'view/?f=%s'%path

    @property
    def edit_link(self):
        path = self.link_path

        if self.is_directory:
            return '?f=%s'%path
        else:
            return 'edit/?f=%s'%path

    @property
    def delete_link(self):
        path = self.link_path

        return 'delete/?f=%s'%path

    @property
    def rename_link(self):
        path = self.link_path

        return 'rename/?f=%s'%path

    @property
    def move_link(self):
        path = self.link_path

        return 'move/?f=%s'%path

    @property
    def mimetype(self):
        if not self._mimetype:
            if self.is_directory:
                self._mimetype = 'Directory'
            else:
                self._mimetype = mimetypes.guess_type(self.full_path)[0]

        return self._mimetype

    @property
    def size(self):
        if not self._size:
            self._size = os.path.getsize(self.full_path)

        return self._size

    def set_content(self, content):
        if self.is_directory:
            return ''

        fp = file(self.full_path, 'w')
        fp.write(content)
        fp.close()

        return True

    @property
    def content(self):
        if self.is_directory:
            return ''

        fp = file(self.full_path)
        ret = fp.read()
        fp.close()

        return ret

    def content_as_text(self):
        return self.content

    def content_as_list(self):
        return self.content_as_text().split('\n')

    def delete(self):
        os.unlink(self.full_path)

    def rename(self, new_name):
        old_name = self.full_path
        new_name = self.path + '/' + new_name
        os.rename(old_name, new_name)

    def move(self, new_path):
        old_name = self.full_path
        new_name = new_path + '/' + self.filename
        shutil.move(old_name, new_name)

    @property
    def is_text(self):
        return self.mimetype.startswith('text/') or self.mimetype == 'application/x-javascript'

    @property
    def is_image(self):
        return self.mimetype.startswith('image/')

    @property
    def is_editable(self):
        return self.is_text

